package com.kmxd.ams.core.common.enums;

import cn.hutool.core.text.CharSequenceUtil;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.kmxd.ams.core.common.constant.ErrorCode;
import com.kmxd.ams.core.exception.BizException;
import java.util.Arrays;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 逻辑操作
 *
 * @author nickbi
 */
@Getter
@AllArgsConstructor
public enum OperatorEnum {

  /** 相等 */
  EQ("equal", "=", "等于"),
  NOT_EQ("notequal", "!=", "不等于"),
  EMPTY("empty", "is null", "为空"),
  NOT_EMPTY("notempty", "is not null", "不为空"),
  GE_EQ("greaterthanequal", ">=", "大于等于"),
  GE("greaterthan", ">", "大于"),
  LE_EQ("lessthanequal", "<=", "小于等于"),
  LE("lessthan", "<", "小于"),
  LIKE("like", "like", "包含"),
  NOT_LIKE("notlike", "not like", "不包含"),
  ;

  private String code;
  private String op;
  private String desc;

  @JsonValue
  public String getCode() {
    return code;
  }

  public static OperatorEnum convert(String type) {
    return Arrays.stream(values())
        .filter(x -> CharSequenceUtil.equalsIgnoreCase(x.getCode(), type))
        .findAny()
        .orElseThrow(() -> BizException.instance(ErrorCode.NOT_FOUND, "未找到档案状态：" + type));
  }

  @JsonCreator
  public static OperatorEnum getValue(String type) {
    return Arrays.stream(values())
        .filter(
            x ->
                CharSequenceUtil.equalsIgnoreCase(x.getCode(), type)
                    || CharSequenceUtil.equalsIgnoreCase(x.name(), type))
        .findAny()
        .orElse(null);
  }
}
